package codeTop;

public class Class43_1 {

    public String multiply(String num1, String num2) {

       if (num1.equals("0")||num2.equals("0")){
           return "0";
       }
        int m = num1.length();
        int n = num2.length();

        String ans="";
        //TODO:2024/1/14 需要计算的层数
        for (int i = n-1; i >=0 ; i--) {
            StringBuffer sb = new StringBuffer();
            //TODO:2024/1/14 需要补充0的个数
            int add=0;
            //TODO:2024/1/14  这点补充0  不太好理解
            for (int j =n-1; j >i ; j--) {
                sb.append(0);
            }
                //TODO:2024/1/14 计算每一层
               int x= num2.charAt(i)- '0';
               //TODO:2024/1/14 计算每一层的数值
                for (int k = m-1; k >=0 ; k--) {
                    int y=num1.charAt(k)-'0';
                    int temp=x*y+add;
                    sb.append(temp%10);
                    add=temp/10;
                }
                if (add!=0){
                    sb.append(add);
                }
                String s2 = sb.reverse().toString();
                ans=addNow(ans,s2);
            }
        return  ans;
    }


    //TODO:2024/1/14  两个字符串相加 不使用字符串转数字实现加法的方法
    public  String  addNow(String s1,String s2){
        int i = s1.length()-1;
        int j = s2.length()-1;
        int add=0;
        StringBuffer stringBuffer = new StringBuffer();
        while (i>=0||j>=0){
          int x=  i>=0?s1.charAt(i)-'0':0;
          int y=j>=0?s2.charAt(j)-'0':0;
          int temp=x+y+add;
          stringBuffer.append(temp%10);
          add=temp/10;
          i--;
          j--;
        }
        if (add!=0){
            stringBuffer.append(add+"");
        }
        return   stringBuffer.reverse().toString();
    }

    public static void main(String[] args) {
        Class43_1 class43_1 = new Class43_1();
        String multiply = class43_1.multiply("9", "99");
        System.out.println(multiply);

    }





}
